package com.ruoyi.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class CouponsListener {
    // 重点：一定要监听的是死信队列!!!
    @RabbitListener(queues = "coupons_dlx_queue")
    public void listenerMessage(Message message, Channel channel) throws
            IOException, InterruptedException {
        try {
            // 1、获取消息
            System.out.println("接收到的消息：" + new String(message.getBody()));
            // 2、处理业务逻辑
            System.out.println("处理业务逻辑...");
            System.out.println("根据订单id查询其状态...");
            System.out.println("判断状态是否为支付成功");
            System.out.println("取消订单，回滚库存....");
            /*
             * 3、无异常，手动签收：
             * */
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),
                    true);
        } catch (Exception e) {
            /*
             * 4、出现异常，拒绝签收，重回队列
             * */
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),
                    true, true);
        }
    }
}